//
//  CCTabbarViewController.m
//  CCLoveBee
//
//  Created by 陈科 on 2017/2/18.
//  Copyright © 2017年 CK. All rights reserved.
//

#import "CCTabbarViewController.h"
#import "CCNavigationController.h"

@interface CCTabbarViewController ()

@end

@implementation CCTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //实例化一个可变数组
    NSMutableArray *arrM = [NSMutableArray array];
    
    //添加
    [arrM addObject:[self addChildControllerWith:@"CCHomeViewController" title:@"首页" normalImage:@"v2_home"]];
    [arrM addObject:[self addChildControllerWith:@"CCLightMarketViewController" title:@"闪电超市" normalImage:@"v2_order"]];
    [arrM addObject:[self addChildControllerWith:@"CCShoppingCarViewController" title:@"购物车" normalImage:@"shopCart"]];
    [arrM addObject:[self addChildControllerWith:@"CCMineViewController" title:@"我的" normalImage:@"v2_my"]];
    //赋值
    self.viewControllers = arrM;
}

- (CCNavigationController *)addChildControllerWith:(NSString *)clsName title:(NSString *)title normalImage:(NSString *)normalImage {
    
    // 1. 把字符串转为class
    Class cls = NSClassFromString(clsName);
    
    // 2. 实例化控制器
    UIViewController *controller = [[cls alloc] init];
    
    // 3. 设置控制器的属性
    // 3.1 设置title
    //    controller.navigationItem.title
    //    controller.tabBarItem.title
    // 这种设置相当于上面两行代码的效果
    controller.title = title;
    
    // 3.2 设置普通状态的image
    controller.tabBarItem.image = [UIImage imageNamed:normalImage];
    
    // 3.3 设置选中的图片
    NSString *selName = [normalImage stringByAppendingString:@"_r"];
    
    UIImage *selImage = [[UIImage imageNamed:selName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.selectedImage = selImage;
    // 3.4 643205 设置选中状态的文字的颜色
//    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x643205)} forState:UIControlStateSelected];
    
    // 4. 实例化导航控制器
    CCNavigationController *nav = [[CCNavigationController alloc] initWithRootViewController:controller];
    
    return nav;
}



@end
